package com.neu.questionnaire_system.netty;

import com.alibaba.fastjson.JSON;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class ChatWebSocketHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
    private static final Logger log = LoggerFactory.getLogger(ChatWebSocketHandler.class);
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("与客户端建立连接，通道开启");
        //添加到通道组
        ChatChannelHandlerPool.channelGroup.add(ctx.channel());

    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        log.info("与客户端连接断开，通道关闭");
        ChatChannelHandlerPool.channelGroup.remove(ctx.channel());
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        //首次连接是FullHttpRequest，处理参数 by zhengkai.blog.csdn.net
        if (msg instanceof FullHttpRequest) {
            /*FullHttpRequest request = (FullHttpRequest) msg;
            String uri = request.uri();

            Map paramMap=getUrlParams(uri);
            System.out.println("接收到的参数是："+JSON.toJSONString(paramMap));
            //如果url包含参数，需要处理
            if(uri.contains("?")){
                String newUri=uri.substring(0,uri.indexOf("?"));
                System.out.println(newUri);
                request.setUri(newUri);
            }*/
            System.out.println(1234);
        }else if(msg instanceof TextWebSocketFrame){
            //正常的TEXT消息类型
            TextWebSocketFrame frame=(TextWebSocketFrame)msg;
            System.out.println("客户端收到服务器数据：" +frame.text());
            AttributeKey<String> attributeKey = AttributeKey.valueOf("Id");
            AttributeKey<String> attributeKey2 = AttributeKey.valueOf("Id2");
            String id = frame.text().split("-")[0];
            String id2 = frame.text().split("-")[1];
            Attribute<String> attribute = ctx.channel().attr(attributeKey);
            Attribute<String> attribute2 = ctx.channel().attr(attributeKey2);
            attribute.set(id);
            attribute2.set(id2);
            for (Channel channel:ChatChannelHandlerPool.channelGroup)
            {
                System.out.println(channel.attr(attributeKey).get());
                if(channel.attr(attributeKey).get().equals(id2)) {

                    String[] tem = frame.text().split("-");
                    channel.writeAndFlush(new TextWebSocketFrame(tem[0]+"-"+tem[2]));
                    break;
                }
            }


        }
        super.channelRead(ctx, msg);
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, TextWebSocketFrame textWebSocketFrame) throws Exception {

    }

    private void sendMessage(ChannelHandlerContext ctx, String msg){
        ctx.channel().writeAndFlush(new TextWebSocketFrame(msg));
    }
}